<?php
namespace app\smallapp\controller;
use app\common\controller\BaseApi;
/*小程序接口--会籍端*/
class MemberShip extends BaseApi{
	function _initialize() {
		if(!request()->isPost()){
			return $this->api_ajax_error("","非法访问");
		}
		$time=input("time/d","");
		$sign=input("sign/s","");
		if(empty(input("debug"))){
			if(($time+300)<time()||($time-300)>time()){//接口调用时间操过5分钟
			return $this->api_ajax_error("","链接已经过期");
			}
			if($sign!=md5(md5("OcmSmallApp")."_".$time)){
				return $this->api_ajax_error("","签名错误！");
			}
		}
    }
	/**
	 * 注册
	 * http://smartgym.com/smallapp/MemberShip/register
	 * */
	function Register(){
		$moblie=input("mobile/s");
		$wx_appid=input("wx_addid/s");
		$psw=input("psw/s");
		if(strlen($moblie)!=11){
			return $this->api_ajax_error("","手机号码错误！");
		}
		if(strlen($psw)<5){
			return $this->api_ajax_error("","密码长度不足5位！");
		}
		$model=db("GymEmployee")->where("mobile='$moblie'")->find();
		if(empty($model)){
			return $this->api_ajax_error("","会籍不存在！");
		}else{
			if($model['web_open_up']!=1){
				return $this->api_ajax_error("","您暂无此权限");
			}
			if(empty($model['wx_appid'])){
				db("GymEmployee")->where("mobile='$moblie'")->setField(array("wx_appid"=>$wx_appid,"psw"=>$psw));
				return $this->api_ajax_success("","成功");
			}else{
				return $this->api_ajax_error("","此号码已经注册");
			}
		}
	}
	/*
	 * 获取短信验证码
	 * http://smartgym.com/smallapp/MemberShip/GetCode
	 * */
	function GetCode(){
		$mobile=input("mobile/s");
		$model=db("GymEmployee")->where("mobile='$mobile'")->find();
		if(empty($model)){
			return $this->api_ajax_error("","会籍不存在！");
		}else{
			if($model['web_open_up']!=1){
				return $this->api_ajax_error("","您暂无此权限");
			}
			$result=send_msg_code($mobile);
			if($result['flag']){
				return $this->api_ajax_success($result['data'],"成功");
			}else{
				return $this->api_ajax_error($result['data'],$result['msg']);
			}
		}
	} 
	/**
	 * 忘记密码 
	 *  http://smartgym.com/smallapp/MemberShip/UpdatePsw
	 */
	function UpdatePsw(){
		$mobile=input("mobile/s");
		$new_psw=input("new_psw/s");
		$old_psw=input("old_psw/s");
		if(strlen(trim($new_psw))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		$id=db("GymEmployee")->where("mobile='$mobile' and psw='$old_psw'")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","账号或密码错误");
		}else{
			db("GymEmployee")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success();
		}
	}
	/**
	 * 修改密码 
	 *  http://smartgym.com/smallapp/MemberShip/ForgetPsw
	 */
	function ForgetPsw(){
		$mobile=input("mobile/s");
		$new_psw=input("new_psw/s");
		if(strlen(trim($new_psw))<5){
			return $this->api_ajax_error("","新密码长度必须大于5位");
		}
		$id=db("GymEmployee")->where("mobile='$mobile'")->value("id");
		if(empty($id)){
			return $this->api_ajax_error("","账号不存在");
		}else{
			db("GymEmployee")->where("id=$id")->setField(array("psw"=>$new_psw));
			return $this->api_ajax_success();
		}
	}
	
	/*
	 * 获取店铺信息
	 * http://smartgym.com/smallapp/MemberShip/GetShopInfo
	 * */
	function GetShopInfo(){
		$shop_id=input("shop_id/d");
		$shop=db("Shop")->where("id=$shop_id")->field("id,shop_name,logo,create_time")->find();
		if(empty($shop)){
			return $this->api_ajax__error("","错误的店铺id");
		}else{
			return $this->api_ajax_success($shop,"成功");
		}
	} 
	
	/**
	 * 登录 
	 * http://smartgym.com/smallapp/MemberShip/Login
	 */
	function Login(){
		$mobile=input("mobile/s");
		$psw=input("psw/s");
		$shop_id=input("shop_id");
		if(empty($shop_id)){
			return $this->api_ajax_error("","shop_id 为空");
		}
		$employee=db("GymEmployee")->where("mobile='$mobile' and psw='$psw' and type=2 and shop_id=$shop_id")->field("id,shop_id,name,mobile,position,sex,icon")->find();
		if(empty($employee)){
			return $this->api_ajax_error("","账号或密码错误");
		}else{
			return $this->api_ajax_success($employee,"成功");
		}
	}
	
	/**
	 * 统计数据
	 * http://smartgym.com/smallapp/MemberShip/StatisticData
	 */
	function StatisticData(){
		$id=input("id");
		$shop_id=input("shop_id");
		return $this->api_ajax_success($this->_StatisticData($id, $shop_id),"成功");
	}
	/**
	 * 新增会员
	 *  http://smartgym.com/smallapp/MemberShip/AddMember
	 * */
	function AddMember(){
		$mobile=input("mobile/s");
		$shop_id=input("shop_id/d");
		if(empty($shop_id)){
			return $this->api_ajax_error("","参数错误");
		}
		if(strlen($mobile)!=11){
			return $this->api_ajax_error("","手机号码格式错误");
		}
		$count=db("GymMember")->where("mobile='$mobile' and shop_id=$shop_id")->count(1);
		if(!empty($count)){
			return $this->api_ajax_error("","手机号码已经存在");
		}else{
			$member_ship_id=db("GymMember")->insertGetId(
				array("name"=>input("name"),"mobile"=>input("mobile"),"sex"=>input("sex/d"),
					  "occupation"=>input("occupation"),"addr"=>input("addr"),"id_card"=>input("id_card"),"come_from"=>input("come_from"),
					  "shop_id"=>input("shop_id"),"member_ship_id"=>input("id"),"descs"=>input("descs")
			));
			//新增一条根据记录
			action("smallapp/FollowUp/add",[input("id"),$member_ship_id,$shop_id]);
			return $this->api_ajax_success();
		}
	}
	/**
	 * 根据电话号码获取店铺
	 * */
	function GetShopList(){
		$mobile=input("mobile");
		if(strlen($mobile)!=11){
			return $this->api_ajax_error("","手机号码格式错误");
		}
		$list=db("GymEmployee")->alias("a")->field("b.id,b.shop_name")->join([["m_shop b","a.shop_id=b.id","left"]])->where("a.mobile='$mobile'  and a.type=2")->select();
		if(empty($list)){
			return $this->api_ajax_error([],"无效的手机账号");
		}else{
			return $this->api_ajax_success($list);
		}
	}
	
	/**
	 * 会员列表
	 *  http://smartgym.com/smallapp/MemberShip/MemberList
	 * */
	function MemberList(){
		$search_type=input("search_type/d",0);//查找类型
		$shop_id=input("shop_id/d",0);//店铺id
		$page=input("page",1);//第几页
		$id=input("id/d",0);//会籍id
		$status=input("status/d",0);//0 所有客户 1有效   2未办卡
		$field="a.gym_card_name,a.gym_card_type,a.start_date,a.end_date,a.gym_member_id,b.name,b.id,b.mobile,b.mobile,b.sex";
		if(empty($search_type)){//查询所有
		    $status_and="";
		    if(!empty($status)){
		    	if($status==1){
		    		$status_and="and id  in(select gym_member_id from m_gym_order where member_ship_id=$id)";	
		    	}elseif($status==2){
		    		$status_and="and id  not in(select gym_member_id from m_gym_order where member_ship_id=$id)";
		    	}
		    }
			$list=db("GymMember")->where("member_ship_id=$id $status_and")->field("name,mobile,sex")->order("id desc")->page($page,10)->select();
		}elseif($search_type==1){//到期
			$list=db("GymOrder")->alias("a")->join([['m_gym_member b',"a.gym_member_id=b.id and b.shop_id=$shop_id","left"]])->where("a.member_ship_id=$id and a.shop_id=$shop_id and a.is_delete=0  and date_format(now(),'%Y-%m')= date_format(a.end_date,'%Y-%m')")->field($field)->order("id desc")->page($page,10)->select();
		}elseif($search_type==2){//次耗尽
			$list=db("GymOrder")->alias("a")->join([['m_gym_member b',"a.gym_member_id=b.id and b.shop_id=$shop_id","left"]])->where("a.member_ship_id=$id and a.shop_id=$shop_id and a.is_delete=0 and a.gym_card_type=1 and a.able_count<1 ")->field($field)->order("id desc")->page($page,10)->select();
		}elseif($search_type==3){//金耗尽
			$list=db("GymOrder")->alias("a")->join([['m_gym_member b',"a.gym_member_id=b.id and b.shop_id=$shop_id","left"]])->where("a.member_ship_id=$id and a.shop_id=$shop_id and a.is_delete=0 and a.gym_card_type=3 and a.able_money<1 ")->field($field)->order("id desc")->page($page,10)->select();
		}elseif($search_type==4){//长时间未到场
			$list=db("GymOrder")->alias("a")->join([['m_gym_member b',"a.gym_member_id=b.id and b.shop_id=$shop_id","left"]])->where("a.member_ship_id=$id and a.shop_id=$shop_id and a.is_delete=0 and date_sub(CURDATE(),INTERVAL 1 MONTH)>DATE_FORMAT(a.create_time,'%Y-%m-%d') and a.card_no not in(select distinct(card_no) from m_behavior_log where shop_id=$shop_id and date_sub(CURDATE(),INTERVAL 1 MONTH)<=date(in_time))")->field($field)->order("id desc")->page($page,10)->select();
		}elseif($search_type==5){//长时间未跟进
			$now_day=date("Y-m-d",strtotime("-1 month"))." 00:00:00";
			$list=db("GymFollowUp")->fetchsql(FALSE)->where("member_ship_id=$id and type=1")->group("member_id")->having("max(create_time) < '$now_day'")->page($page,10)->column("member_id");
			if(empty($list)){
				return $this->api_ajax_error("","抱歉！没有更多的数据");
			}
			$member_ids=implode(",", $list);
			$members=db("GymMember")->field("name,sex,mobile,id as gym_member_id, '' as gym_card_name,'' as gym_card_type")->where("id in($member_ids)")->select();
			if(empty($members)){
				return $this->api_ajax_error("","抱歉！没有更多的数据");
			}else{
				$list=db("GymOrder")->where("gym_member_id in($member_ids) and is_delete=0")->group("gym_member_id")->column("gym_member_id,gym_card_name,gym_card_type");
				foreach ($members as $key => $value) {
					$members[$key]['gym_card_name']=$list[$value['gym_member_id']]['gym_card_name'];
					$members[$key]['gym_card_type']=$list[$value['gym_member_id']]['gym_card_type'];
				}
			}
			return $this->api_ajax_success($members,"成功");
		}else{
			return $this->api_ajax_error("","参数类型错误");
		}
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}
		return $this->api_ajax_success($list,"成功");
	}
	/**
	 * 跟进列表
	 * http://smartgym.com/smallapp/MemberShip/FollowUpList
	 * */
	function FollowUpList(){
		$this->_FollowUpList();
	}
    /**
	 * 客户跟进
	 * http://smartgym.com/smallapp/MemberShip/GuestFollowUp
	 * */
	function GuestFollowUp(){
		$this->_GuestFollowUp();
	}
	/**
	 * 添加预约/跟进
	 * http://smartgym.com/smallapp/MemberShip/GuestFollowUpPost
	 * */
	function GuestFollowUpPost(){
		$this->_GuestFollowUpPost();
	}
	/**
	 * 修改预约
	 * */
	 function GuestFollowUpEdit(){
	 	$this->_GuestFollowUpEdit();
	 }
	 
	/**
	 * 预约/跟进查询
	 *  http://smartgym.com/smallapp/MemberShip/Search
	 * */
	function Search(){
		$this->_Search(); 	
	 }
	/**
	 *会员详情
	 * */
	function Detail(){
	    $this->_Detail();	
	}
    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    function _GuestFollowUpEdit(){
		$data['order_time']=input("order_time");
		$data['id']=input("follow_id/d");
		$data['remarks']=input("remarks");
		db("GymFollowUp")->update($data);
		$this->api_ajax_success();
    }
    
    function _Detail(){
    	$member_id=input("member_id/d");
		$search_type=input("search_type/d",1);
		$member=db("GymMember")->field("mobile,sex,id,name")->find($member_id);
		if(empty($member)){
			return $this->api_ajax_error("无效的会员");
		}else{
			$follow_up=db("GymFollowUp")->where("member_id=$member_id")->field("sum(case when type=2 then 1 else 0 end) as order_count,sum(case when type=1 then 1 else 0 end) as follow_count,max(order_time) as order_time,max(create_time) as follow_time")->select();
			if(empty($follow_up)){
				$follow_up=["order_count"=>$order_count,"order_time"=>null,"follow_count"=>0,"follow_time"=>null];
			}else{
				$follow_up=$follow_up[0];
			}
			if($search_type==1){//潜在会员
				$order=array("gym_card_name"=>"","end_date"=>"","surplus"=>0);
			}else{//有效会员
				$order=db("GymOrder")->where("gym_member_id=$member_id and is_delete=0")->field("gym_card_type,gym_card_name,end_date,able_money,able_count,start_date")->order("id desc")->find();
				if(empty($order)){
					return $this->api_ajax_error("不是有效会员");
				}
				if($order['gym_card_type']==1){//次卡
					$order['surplus']=$order['able_count'];
				}else if(in_array($order['gym_card_type'], ["4","2"])){//错峰卡,期限卡
					$order['surplus']=$this->_get_able_day($order['start_date'],$order['end_date']);
				}else if($order['gym_card_type']==3){//储值卡
					$order['surplus']=$order['able_money'];
				}
				unset($order['able_money']);
				unset($order['able_count']);
				unset($order['start_date']);
			}
			return $this->api_ajax_success(array_merge($member,$follow_up,$order));
		}
    }
    function _get_able_day($start,$end){
	   if(strcmp($start,$end)<0){
	   	   if(strcmp(date("Y-m-d"),$start)<0){//未开卡
	   	   		$day=(strtotime("$end")-strtotime("$start"))/(24*60*60); 
	   	   }else{
	   	   	   $day=(strtotime("$end")-strtotime(date("Y-m-d")))/(24*60*60);
	   	   }
	   }else{
	   	  $day=0;
	   }	
	   return $day;
	}
    function _Search(){
    	$search_type=input("search_type/d",1);
		$search_day=input("search_day");
		$keywords=input("keywords/s");
		$member_ship_id=input("member_ship_id/d",0);
		$shop_id=input("shop_id/d",0);
		
		//时间查询
		$time_and="";
		if(!empty($search_day)){
				if($search_type==2){//预约
					$time_and=" and  date_format(order_time, '%Y-%m-%d') like '%$search_day%'";
				}else{//跟进
					$time_and=" and date_format(create_time, '%Y-%m-%d') like '%$search_day%'";
				}
		}
		
		//查询统计
		$list=db("GymFollowUp")->fetchsql(FALSE)->where(" type=$search_type and shop_id=$shop_id and member_ship_id=$member_ship_id  $time_and ")->group("member_id")->column("member_id,sum(case when type=1 then 1 else 0 end) as follow_count,sum(case when type=2 then 1 else 0 end) as order_count,SUBSTRING_INDEX(GROUP_CONCAT( remarks ORDER BY id DESC),'\,',1) as remarks");
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}
		
		$member_ids="";
	    foreach ($list as $key => $value) {
			$member_ids[]=$value['member_id'];			    	
	    }
		$member_ids=implode(",", $member_ids);
		//最后一次跟进
		$max_type_1=db("GymFollowUp")->where("member_id in ($member_ids) and type=1")->group("member_id")->column("member_id,max(create_time) as create_time");
		//最后一次预约
		$max_type_2=db("GymFollowUp")->where("member_id in ($member_ids) and type=2")->group("member_id")->column("member_id,max(order_time) as order_time");
		
		$members=db("GymMember")->field("name,sex,mobile,id as member_id")->where("id in($member_ids) and (name like '%$keywords%' or mobile like'%$keywords%')")->select();
		foreach ($members as $key => $value) {
				$members[$key]['follow_count']=$list[$value['member_id']]['follow_count'];
				$members[$key]['order_count']=$list[$value['member_id']]['order_count'];
				$members[$key]['remarks']=$list[$value['member_id']]['remarks'];
				if(isset($max_type_1[$value['member_id']])){
					$members[$key]['create_time']=$max_type_1[$value['member_id']];	
				}else{
					$members[$key]['create_time']="";
				}
				if(isset($max_type_2[$value['member_id']])){
					$members[$key]['order_time']=$max_type_2[$value['member_id']];
				}else{
					$members[$key]['order_time']="";
				}								    	
		}
		if(empty($members)){
				return $this->api_ajax_error("","抱歉！没有更多的数据");
			}
		$this->api_ajax_success($members);
    }
    function _GuestFollowUpPost(){
    	$data['follow_up_type']=input("follow_up_type");
		$data['order_time']=input("order_time");
		$data['shop_id']=input("shop_id");
		$data['member_ship_id']=input("member_ship_id");
		$data['member_id']=input("member_id");
		$data['phone_status']=input("phone_status");
		$data['type']=input("type");
		$data['remarks']=input("remarks");
		$data['remarks1']=input("remarks1","");
		if(input("type")==1){//跟进
			$data['order_time']=null;
		}else{
			if(empty(input("order_time"))){
				$this->api_ajax_error("","请填写预约时间");
			}
		}
		db("GymFollowUp")->insert($data);
		$this->api_ajax_success();
    }
	function _GuestFollowUp(){
		$member_ship_id=input("member_ship_id");
		$date['mc_sell_followup']=db()->query("select count(1) as count from m_gym_follow_up where type=1 and member_ship_id=$member_ship_id and member_id not in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")[0]['count'];
		$date['mc_service_followup']=db()->query("select count(1) as count from m_gym_follow_up where type=1  and member_ship_id=$member_ship_id and member_id in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")[0]['count'];
		$date['mc_followup']=$date['mc_sell_followup']+$date['mc_service_followup'];
		$date['mc_order']=db()->query("select count(1) as count from m_gym_follow_up where type=2 and member_ship_id=$member_ship_id")[0]['count'];
		
		$date['mc_sell_followup_today']=db()->query("select count(distinct(member_id)) as count from m_gym_follow_up where type=1 and member_ship_id=$member_ship_id and date_format(create_time, '%Y%m%d')=date_format(now(), '%Y%m%d')  and member_id not in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")[0]['count'];
		$date['mc_service_followup_today']=db()->query("select count(distinct(member_id)) as count from m_gym_follow_up where type=1  and member_ship_id=$member_ship_id  and date_format(create_time, '%Y%m%d')=date_format(now(), '%Y%m%d') and member_id in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")[0]['count'];
		$date['mc_sell_order_today']=db()->query("select count(distinct(member_id)) as count from m_gym_follow_up where type=2 and member_ship_id=$member_ship_id and  date_format(order_time, '%Y%m%d')=date_format(now(), '%Y%m%d') and  member_id not in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")[0]['count'];
		$date['mc_service_order_today']=db()->query("select count(distinct(member_id)) as count from m_gym_follow_up where type=2 and member_ship_id=$member_ship_id and  date_format(order_time, '%Y%m%d')=date_format(now(), '%Y%m%d') and member_id in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)" )[0]['count'];
		$this->api_ajax_success($date);
	}
	function _FollowUpList(){
		$keyword=input("keyword","");
		$search_day=input("search_day");
		$days=input("days/d",0);
		$shop_id=input("shop_id");
		$member_ship_id=input("member_ship_id");
		$search_type=input("search_type",1);//1销售跟进  2服务跟进  3预约跟进 
		$where_and="";	
		if($search_type==1){
			$where_and=" and member_id not in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id) ";
		}elseif($search_type==2){
			$where_and=" and member_id in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id) "	;
		}
		//时间查询
		$time_and="";
		if(!empty($search_day)){
				if($search_type==3){
					$time_and=" and  date_format(order_time, '%Y-%m-%d') like '%$search_day%'";
				}else{
					$time_and=" and date_format(create_time, '%Y-%m-%d') like '%$search_day%'";
				}
		}
		if(!empty($days)){
				$days=$days+1;
				$start=date("Y-m-d",strtotime("-$days day"));
				if($search_type==3){
					$time_and=" and order_time='$start' ";
				}else{
					$time_and=" and date_format(create_time, '%Y-%m-%d')='$start'";
				}
		}
		
		if($search_type==3){//预约
			$list=db("GymFollowUp")->fetchsql(FALSE)->where("shop_id=$shop_id and member_ship_id=$member_ship_id and type=2 $time_and")->group("member_id")->column("member_id,count(member_id) as count,max(order_time) as create_time");
		}else{
			$list=db("GymFollowUp")->fetchsql(FALSE)->where("shop_id=$shop_id and member_ship_id=$member_ship_id and type=1 $where_and $time_and")->group("member_id")->column("member_id,count(member_id) as count,max(create_time) as create_time");
		}
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}else{
			$member_ids="";
		    foreach ($list as $key => $value) {
				$member_ids[]=$value['member_id'];			    	
		    }
			$member_ids=implode(",", $member_ids);
			
			$members=db("GymMember")->field("name,sex,mobile,id as member_id")->where("id in($member_ids) and (name like '%$keyword%' or mobile like'%$keyword%')")->select();
			foreach ($members as $key => $value) {
				$members[$key]['count']=$list[$value['member_id']]['count'];
				$members[$key]['create_time']=$list[$value['member_id']]['create_time'];			    	
		    }
			if(empty($members)){
				return $this->api_ajax_error("","抱歉！没有更多的数据");
			}
			return $this->api_ajax_success($members,"成功");
		}
	}
	/**
	 * 服务跟进列表
	 * http://smartgym.com/smallapp/MemberShip/ServiceFollowUpList
	 * */
	function _ServiceFollowUpList(){
		$keyword=input("keyword","");
		$search_day=input("search_day");
		$days=input("days");
		$shop_id=input("shop_id");
		$member_ship_id=input("member_ship_id");
		$list=db("GymFollowUp")->where("shop_id=$shop_id and member_ship_id=$member_ship_id and member_id in(select gym_member_id from m_gym_order where member_ship_id=$member_ship_id)")->group("member_id")->column("member_id,count(member_id) as count,max(create_time) as create_time");
		if(empty($list)){
			return $this->api_ajax_error("","抱歉！没有更多的数据");
		}else{
			$member_ids="";
		    foreach ($list as $key => $value) {
				$member_ids[]=$value['member_id'];			    	
		    }
			$member_ids=implode(",", $member_ids);	
			$members=db("GymMember")->field("name,sex,mobile,id as member_id")->where("id in($member_ids) and (name like '%$keyword%' or mobile like'%$keyword%')")->select();
			foreach ($members as $key => $value) {
				$members[$key]['count']=$list[$value['member_id']]['count'];
				$members[$key]['create_time']=$list[$value['member_id']]['create_time'];			    	
		    }
			if(empty($members)){
				return $this->api_ajax_error("","抱歉！没有更多的数据");
			}
			return $this->api_ajax_success($members,"成功");
		}
	}
	function _StatisticData($id,$shop_id){
		$today_sales_volume=db("GymOrderHistory")->where("member_ship_id=$id and shop_id=$shop_id and operation_type NOT IN (6,7,8) and  date_format(now(),'%Y-%m-%d')= date_format(create_time,'%Y-%m-%d')")->sum("pay_money");
        $year_month = date("Ym", time());
		$month_sales_volume=db("gym_employee_achievement")->where(array("shop_id" => $shop_id, "year_month" => $year_month, "employee_id" => $id))->value("achievement");
		$month_later = date("Y-m-d", strtotime("1 month"));
		$today = date("Y-m-d");
		$recent_expire=db("GymOrder")->where(" member_ship_id=71 and shop_id=71 and is_delete=0 and end_date <= '$month_later' and end_date >= '$today'")->count(1);
		$count_expire=db("GymOrder")->where(" member_ship_id=$id and shop_id=$shop_id and is_delete=0 and gym_card_type=1 and able_count<1 ")->count(1);
		$money_expire=db("GymOrder")->where(" member_ship_id=$id and shop_id=$shop_id and is_delete=0 and gym_card_type=3 and able_money<1 ")->count(1);
		$no_arrival_long_time=db("GymOrder")->fetchsql(FALSE)->where(" member_ship_id=$id and shop_id=$shop_id and is_delete=0 and date_sub(CURDATE(),INTERVAL 1 MONTH)>DATE_FORMAT(create_time,'%Y-%m-%d') and  card_no not in(select distinct(card_no) from m_behavior_log where shop_id=$shop_id and date_sub(CURDATE(),INTERVAL 1 MONTH)<=date(in_time))")->count(1);
		//长时间未根据
		$now_day=date("Y-m-d",strtotime("-1 month"))." 00:00:00";
		$no_followup_long_time=db("GymFollowUp")->where("member_ship_id=$id and type=1")->group("member_id")->having("max(create_time) < '$now_day'")->count(1);
		
		$result="";
		if(empty($month_sales_volume)){
			$result['month_sales_volume']=0;
		}else{
			$result['month_sales_volume']=$month_sales_volume;
		}
		if(empty($today_sales_volume)){
			$result['today_sales_volume']=0;
		}else{
			$result['today_sales_volume']=$today_sales_volume;
		}	
		if(empty($recent_expire)){
			$result['recent_expire']=0;
		}else{
			$result['recent_expire']=$recent_expire;
		}	
		if(empty($count_expire)){
			$result['count_expire']=0;
		}else{
			$result['count_expire']=$count_expire;
		}
		if(empty($money_expire)){
			$result['money_expire']=0;
		}else{
			$result['money_expire']=$money_expire;
		}
		if(empty($no_arrival_long_time)){
			$result['no_arrival_long_time']=0;
		}else{
			$result['no_arrival_long_time']=$no_arrival_long_time;
		}
		if(empty($no_followup_long_time)){
			$result['no_followup_long_time']=0;
		}else{
			$result['no_followup_long_time']=$no_followup_long_time;
		}
		return $result;
	}	 
}
